package com.guoguo.factory;

import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.slf4j.Logger;

import com.guoguo.annotations.PersistenceLog;

public class EntityManagerProvider {
	
	private static final EntityManagerFactory factory = Persistence.createEntityManagerFactory("users");
	
	@Inject 
	@PersistenceLog
	private Logger logger;
	
	@Produces
	EntityManager createEntityManager(){
		EntityManager entityManager = factory.createEntityManager();
		logger.info("{} is produced.", entityManager);
		return entityManager;
	}
	
	//@Disposes对应@Produces，用来释放 Producer Method 占用的资源
	public void close (@Disposes EntityManager em){
		if (em.isOpen()) {em.close();}
		logger.info("{} is diposed.", em);
	}
	
	public static EntityManagerFactory getFactory() {
		return factory;
	}

}
